<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="80px">
      <el-form-item label="被扣款人" prop="memberNick">
        <el-input
          v-model="queryParams.memberNick"
          placeholder="请输入被扣款人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['sl:dm:add']"
        >新增</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="dmList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="被扣款人" align="center" prop="memberNick" >
        <template slot-scope="scope">
          <span v-for="(item,index) in scope.row.slDeductionList" :key="index">
            {{item.memberNick}} {{index === scope.row.slDeductionList.length-1 ? "":","}}
          </span>
        </template>
      </el-table-column>
      <el-table-column label="分别被扣款金额" align="center" prop="dmAmount" />
      <el-table-column label="扣款时间" align="center" prop="dmTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.dmTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="扣款人" align="center" prop="dmNick" />
      <el-table-column label="扣款原因" align="center" prop="dmReason" />
      <el-table-column label="是否结算" align="center" prop="isSettle">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sl_dm_settle" :value="scope.row.isSettle"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['sl:dm:edit']"
          >修改</el-button>
          <el-button
            v-if="scope.row.isSettle!='0'"
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['sl:dm:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改报工项扣款对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="960px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="报工单" prop="rfworkId">
              <el-input v-model="form.rfworkTitle" :readonly="true" placeholder="请选择报工单" >
                <el-button slot="append" icon="el-icon-search" @click="handleAddSlRfwork"></el-button>
              </el-input>
            </el-form-item>
            <RfworkSelect ref="rfworkSelect" @onSelected="rfworkSelected"></RfworkSelect>
          </el-col>
          <el-col :span="12">
            <el-form-item label="报工项" prop="rworkId">
              <el-input v-model="form.rworkTitle" :readonly="true" placeholder="请选择报工项" >
                <el-button slot="append" icon="el-icon-search" @click="handleAddSlRwork"></el-button>
              </el-input>
            </el-form-item>
            <RworkSelect ref="rworkSelect" @onSelected="rworkSelected"></RworkSelect>
          </el-col>
        </el-row>
       <el-form-item label="扣款金额" prop="dmAmount">
         <el-input v-model="form.dmAmount" class="def-input-number" type="number" placeholder="请输入扣款金额" />
       </el-form-item>
       <el-form-item label="扣款原因" prop="dmReason">
         <el-input v-model="form.dmReason" type="textarea" placeholder="请输入内容" />
       </el-form-item>
        <el-form-item label="扣款时间" prop="dmTime">
          {{form.dmTime}}
        </el-form-item>
        <el-form-item label="是否结算" prop="isSettle">
          <dict-tag :options="dict.type.sl_dm_settle" :value="form.isSettle"/>
        </el-form-item>
        <el-divider content-position="center">被扣款人</el-divider>
        <el-table :data="slDeductionList" :row-class-name="rowSlDeductionIndex" ref="slDeduction">
          <el-table-column label="序号" align="center" prop="index" width="50"/>
         <!-- <el-table-column label="用户" prop="memberUser" >
            <template slot-scope="scope">
              {{scope.row.memberUser}}
            </template>
          </el-table-column> -->
          <el-table-column label="操作人" prop="memberNick">
            <template slot-scope="scope">
              {{scope.row.memberNick}}
            </template>
          </el-table-column>
          <el-table-column label="是否结算" prop="isSettle">
            <template slot-scope="scope">
              <dict-tag :options="dict.type.sl_dm_settle" :value="scope.row.isSettle"/>
            </template>
          </el-table-column>
        </el-table>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button v-if="form.isSettle!='0'" type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>


    </el-dialog>
  </div>
</template>

<script>
import { listDm, getDm, delDm, addDm, updateDm } from "@/api/sl/dm";
import {formatDate} from '@/utils/index'
import RfworkSelect from "./rfworklist";
import RworkSelect from "./rworklist";

export default {
  name: "Dm",
  components: {RfworkSelect,RworkSelect},
  dicts: ['sl_dm_settle', 'sl_deduction_settle'],
  data() {
    let watermarkContentVaild=(rule, value, callback)=>{
      if(value==null || value===""){
        callback();
      }else{
        let length=value.length
        if(length>7){
          callback(new Error("输入不能超过7位"))
        }else{
          callback();
        }
      }
    }
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 子表选中数据
      checkedSlDeduction: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 报工项扣款表格数据
      dmList: [],
      // 扣款记录表格数据
      slDeductionList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        rworkId: null,
        dmTime: null,
        dmUser: null,
        dmNick: null,
        rfworkId: null,
        isSettle: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        rfworkId: [
          {required: true,message: "报工单不能为空",trigger: "blur"},
        ],
        rworkId: [
          {required: true,message: "报工项不能为空",trigger: "blur"},
        ],
        dmAmount: [
          {required: true,message: "扣款金额不能为空",trigger: "blur"},
          {validator: watermarkContentVaild, trigger: 'blur'}
        ],
        dmReason: [
          {required: true,message: "扣款原因不能为空",trigger: "blur"},
        ],
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询报工项扣款列表 */
    getList() {
      this.loading = true;
      listDm(this.queryParams).then(response => {
        this.dmList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        dmId: null,
        rworkId: null,
        memberUser: null,
        memberNick: null,
        dmTime: null,
        dmUser: null,
        dmNick: null,
        dmAmount: null,
        rfworkId: null,
        dmReason: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        isSettle: 1,
        rworkTitle: null,
        rfworkTitle: null
      };
      this.slDeductionList = [];
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.dmId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.form.dmTime = formatDate(new Date());
      this.form.dmUser = this.$store.state.user.name;
      this.open = true;
      this.title = "添加报工项扣款";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const dmId = row.dmId || this.ids
      getDm(dmId).then(response => {
        this.form = response.data;
        this.slDeductionList = response.data.slDeductionList;
        this.open = true;
        this.title = "修改报工项扣款";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.slDeductionList = this.slDeductionList;
          if (this.form.dmId != null) {
            updateDm(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addDm(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const dmIds = row.dmId || this.ids;
      this.$modal.confirm('是否确认删除报工项扣款编号为"' + dmIds + '"的数据项？').then(function() {
        return delDm(dmIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
	/** 扣款记录序号 */
    rowSlDeductionIndex({ row, rowIndex }) {
      row.index = rowIndex + 1;
    },
    /** 复选框选中数据 */
    handleSlDeductionSelectionChange(selection) {
      this.checkedSlDeduction = selection.map(item => item.index)
    },
    handleAddSlRfwork() {
      this.$refs.rfworkSelect.showFlag = true;
    },
    handleAddSlRwork() {
      if(null==this.form.rfworkId){
        this.$modal.msgError("请先选择报工单。");
        return;
      }
      this.$refs.rworkSelect.show(this.form.rfworkId);
    },
    rfworkSelected(rows) {
      console.log(rows)
      this.form.rfworkTitle = "["+rows.rfworkDate +"]"+ rows.rfworkNick+"的报工单"
      this.form.rfworkId = rows.rfworkId;
    },
    rworkSelected(rows) {
       console.log(rows)
      this.form.rworkTitle = rows.slModule.moduleName +"-"+ rows.slWorkshop.workshopName +"-"+ rows.slGprocess.gprocessName +"-"+ rows.slProcess.processName
      this.form.rworkId = rows.rworkId;
      this.slDeductionList = rows.slRworkRmemberList.map(item => ({
        "memberUser": item.rmemberUser,
        "memberNick": item.rmemberNick,

      }))

    }
  }
};
</script>
